package genericdemo;
class MyArray<T>{
    Object[] array = new Object[3];
    public void setArray(int pos,T val) {
        array[pos] = val;
    }
    public T getVal(int pos) {
        return (T)array[pos];
    }
}
class MyArray1{
    Object[] array = new Object[3];
    public void setArray(int pos,Object val) {
        array[pos] = val;
    }
    public Object getVal(int pos) {
        return array[pos];
    }
}
public class Test {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();
        myArray.setArray(0,21);//编译时，自动进行类型检查
        Integer val = myArray.getVal(0);//编译时，自动进行类型转换
        MyArray<String> myArray1 = new MyArray<>();
        myArray1.setArray(1,"hello");
        String str = myArray1.getVal(1);
        System.out.println(str);
    }

    public static void main1(String[] args) {
        MyArray1 myArray = new MyArray1();
        myArray.setArray(0,1);
        myArray.setArray(1,"hello");
        String ret1 = (String)myArray.getVal(1);
    }
}
